package com.ccb.sc.member.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccb.sc.member.mapper.AppMemberMedalMapper;
import com.ccb.sc.member.vo.MemberMedalListVo;
import com.ccb.sc.common.modules.member.entity.MemberMedal;
import com.ccb.sc.common.modules.member.enums.MedalType;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员勋章 服务实现类
 * </p>
 *
 * @author ken
 * @since 2022-07-30
 */
@Service
public class AppMemberMedalService extends ServiceImpl<AppMemberMedalMapper, MemberMedal> {

    public List<MemberMedalListVo> findMemberMedalList(Long memberId) {
        List<MemberMedalListVo> list = new ArrayList<>();
        List<MemberMedal> memberMedals = new ArrayList<>();
        if (memberId != null) {
            memberMedals = this.list(Wrappers.<MemberMedal>lambdaQuery().eq(MemberMedal::getMemberId, memberId));
        }
        for (MedalType value : MedalType.values()) {
            MemberMedalListVo vo = new MemberMedalListVo();
            vo.setMedalType(value);
            for (MemberMedal memberMedal : memberMedals) {
                if (value.equals(memberMedal.getMedalType())) {
                    vo.setIsLightUp(true);
                    vo.setIsShow(memberMedal.getIsShow());
                    vo.setLightUpReason(memberMedal.getLightUpReason());
                    vo.setLightUpTime(memberMedal.getLightUpTime().toLocalDate());

                    memberMedal.setIsShow(true);
                    break;
                }
            }
            list.add(vo);
        }
        if (!CollectionUtils.isEmpty(memberMedals)) {
            this.updateBatchById(memberMedals);
        }
        return list;
    }

    public void medalStatus(Long memberId, Integer value) {
        MedalType medalType = MedalType.getByValue(value);
        Assert.notNull(medalType, "勋章类型错误");
        MemberMedal memberMedal = new MemberMedal();
        memberMedal.setIsShow(false);
        this.update(memberMedal, Wrappers.<MemberMedal>lambdaUpdate().eq(MemberMedal::getMemberId, memberId).eq(MemberMedal::getMedalType, medalType));
    }
}
